﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using ZY.IDAL;
using Maticsoft.DBUtility;//Please add references
namespace ZY.SQLServerDAL
{
	/// <summary>
	/// 数据访问类:Tab_Userinfo
	/// </summary>
	public partial class Tab_Userinfo:ITab_Userinfo
	{
		public Tab_Userinfo()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("uid", "Tab_Userinfo"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int uid)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Tab_Userinfo");
			strSql.Append(" where uid=@uid");
			SqlParameter[] parameters = {
					new SqlParameter("@uid", SqlDbType.Int,4)
			};
			parameters[0].Value = uid;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(ZY.Model.Tab_Userinfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Tab_Userinfo(");
			strSql.Append("pid,rid,dp_id,u_userName,u_password,u_trueName,u_power,u_mobilePhone,u_email,u_address,u_state,u_img,u_leve,u_sn,u_note,u_createtime,u_lastime,u_ext1,u_ext2,u_ext3,u_ext4)");
			strSql.Append(" values (");
			strSql.Append("@pid,@rid,@dp_id,@u_userName,@u_password,@u_trueName,@u_power,@u_mobilePhone,@u_email,@u_address,@u_state,@u_img,@u_leve,@u_sn,@u_note,@u_createtime,@u_lastime,@u_ext1,@u_ext2,@u_ext3,@u_ext4)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@pid", SqlDbType.Int,4),
					new SqlParameter("@rid", SqlDbType.Int,4),
					new SqlParameter("@dp_id", SqlDbType.Int,4),
					new SqlParameter("@u_userName", SqlDbType.VarChar,50),
					new SqlParameter("@u_password", SqlDbType.VarChar,50),
					new SqlParameter("@u_trueName", SqlDbType.VarChar,50),
					new SqlParameter("@u_power", SqlDbType.VarChar,8000),
					new SqlParameter("@u_mobilePhone", SqlDbType.VarChar,50),
					new SqlParameter("@u_email", SqlDbType.VarChar,250),
					new SqlParameter("@u_address", SqlDbType.VarChar,250),
					new SqlParameter("@u_state", SqlDbType.Int,4),
					new SqlParameter("@u_img", SqlDbType.VarChar,5000),
					new SqlParameter("@u_leve", SqlDbType.Int,4),
					new SqlParameter("@u_sn", SqlDbType.Int,4),
					new SqlParameter("@u_note", SqlDbType.VarChar,8000),
					new SqlParameter("@u_createtime", SqlDbType.DateTime),
					new SqlParameter("@u_lastime", SqlDbType.DateTime),
					new SqlParameter("@u_ext1", SqlDbType.VarChar,50),
					new SqlParameter("@u_ext2", SqlDbType.VarChar,50),
					new SqlParameter("@u_ext3", SqlDbType.VarChar,50),
					new SqlParameter("@u_ext4", SqlDbType.VarChar,50)};
			parameters[0].Value = model.pid;
			parameters[1].Value = model.rid;
			parameters[2].Value = model.dp_id;
			parameters[3].Value = model.u_userName;
			parameters[4].Value = model.u_password;
			parameters[5].Value = model.u_trueName;
			parameters[6].Value = model.u_power;
			parameters[7].Value = model.u_mobilePhone;
			parameters[8].Value = model.u_email;
			parameters[9].Value = model.u_address;
			parameters[10].Value = model.u_state;
			parameters[11].Value = model.u_img;
			parameters[12].Value = model.u_leve;
			parameters[13].Value = model.u_sn;
			parameters[14].Value = model.u_note;
			parameters[15].Value = model.u_createtime;
			parameters[16].Value = model.u_lastime;
			parameters[17].Value = model.u_ext1;
			parameters[18].Value = model.u_ext2;
			parameters[19].Value = model.u_ext3;
			parameters[20].Value = model.u_ext4;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(ZY.Model.Tab_Userinfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Tab_Userinfo set ");
			strSql.Append("pid=@pid,");
			strSql.Append("rid=@rid,");
			strSql.Append("dp_id=@dp_id,");
			strSql.Append("u_userName=@u_userName,");
			strSql.Append("u_password=@u_password,");
			strSql.Append("u_trueName=@u_trueName,");
			strSql.Append("u_power=@u_power,");
			strSql.Append("u_mobilePhone=@u_mobilePhone,");
			strSql.Append("u_email=@u_email,");
			strSql.Append("u_address=@u_address,");
			strSql.Append("u_state=@u_state,");
			strSql.Append("u_img=@u_img,");
			strSql.Append("u_leve=@u_leve,");
			strSql.Append("u_sn=@u_sn,");
			strSql.Append("u_note=@u_note,");
			strSql.Append("u_createtime=@u_createtime,");
			strSql.Append("u_lastime=@u_lastime,");
			strSql.Append("u_ext1=@u_ext1,");
			strSql.Append("u_ext2=@u_ext2,");
			strSql.Append("u_ext3=@u_ext3,");
			strSql.Append("u_ext4=@u_ext4");
			strSql.Append(" where uid=@uid");
			SqlParameter[] parameters = {
					new SqlParameter("@pid", SqlDbType.Int,4),
					new SqlParameter("@rid", SqlDbType.Int,4),
					new SqlParameter("@dp_id", SqlDbType.Int,4),
					new SqlParameter("@u_userName", SqlDbType.VarChar,50),
					new SqlParameter("@u_password", SqlDbType.VarChar,50),
					new SqlParameter("@u_trueName", SqlDbType.VarChar,50),
					new SqlParameter("@u_power", SqlDbType.VarChar,8000),
					new SqlParameter("@u_mobilePhone", SqlDbType.VarChar,50),
					new SqlParameter("@u_email", SqlDbType.VarChar,250),
					new SqlParameter("@u_address", SqlDbType.VarChar,250),
					new SqlParameter("@u_state", SqlDbType.Int,4),
					new SqlParameter("@u_img", SqlDbType.VarChar,5000),
					new SqlParameter("@u_leve", SqlDbType.Int,4),
					new SqlParameter("@u_sn", SqlDbType.Int,4),
					new SqlParameter("@u_note", SqlDbType.VarChar,8000),
					new SqlParameter("@u_createtime", SqlDbType.DateTime),
					new SqlParameter("@u_lastime", SqlDbType.DateTime),
					new SqlParameter("@u_ext1", SqlDbType.VarChar,50),
					new SqlParameter("@u_ext2", SqlDbType.VarChar,50),
					new SqlParameter("@u_ext3", SqlDbType.VarChar,50),
					new SqlParameter("@u_ext4", SqlDbType.VarChar,50),
					new SqlParameter("@uid", SqlDbType.Int,4)};
			parameters[0].Value = model.pid;
			parameters[1].Value = model.rid;
			parameters[2].Value = model.dp_id;
			parameters[3].Value = model.u_userName;
			parameters[4].Value = model.u_password;
			parameters[5].Value = model.u_trueName;
			parameters[6].Value = model.u_power;
			parameters[7].Value = model.u_mobilePhone;
			parameters[8].Value = model.u_email;
			parameters[9].Value = model.u_address;
			parameters[10].Value = model.u_state;
			parameters[11].Value = model.u_img;
			parameters[12].Value = model.u_leve;
			parameters[13].Value = model.u_sn;
			parameters[14].Value = model.u_note;
			parameters[15].Value = model.u_createtime;
			parameters[16].Value = model.u_lastime;
			parameters[17].Value = model.u_ext1;
			parameters[18].Value = model.u_ext2;
			parameters[19].Value = model.u_ext3;
			parameters[20].Value = model.u_ext4;
			parameters[21].Value = model.uid;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int uid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Tab_Userinfo ");
			strSql.Append(" where uid=@uid");
			SqlParameter[] parameters = {
					new SqlParameter("@uid", SqlDbType.Int,4)
			};
			parameters[0].Value = uid;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string uidlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Tab_Userinfo ");
			strSql.Append(" where uid in ("+uidlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public ZY.Model.Tab_Userinfo GetModel(int uid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 uid,pid,rid,dp_id,u_userName,u_password,u_trueName,u_power,u_mobilePhone,u_email,u_address,u_state,u_img,u_leve,u_sn,u_note,u_createtime,u_lastime,u_ext1,u_ext2,u_ext3,u_ext4 from Tab_Userinfo ");
			strSql.Append(" where uid=@uid");
			SqlParameter[] parameters = {
					new SqlParameter("@uid", SqlDbType.Int,4)
			};
			parameters[0].Value = uid;

			ZY.Model.Tab_Userinfo model=new ZY.Model.Tab_Userinfo();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public ZY.Model.Tab_Userinfo DataRowToModel(DataRow row)
		{
			ZY.Model.Tab_Userinfo model=new ZY.Model.Tab_Userinfo();
			if (row != null)
			{
				if(row["uid"]!=null && row["uid"].ToString()!="")
				{
					model.uid=int.Parse(row["uid"].ToString());
				}
				if(row["pid"]!=null && row["pid"].ToString()!="")
				{
					model.pid=int.Parse(row["pid"].ToString());
				}
				if(row["rid"]!=null && row["rid"].ToString()!="")
				{
					model.rid=int.Parse(row["rid"].ToString());
				}
				if(row["dp_id"]!=null && row["dp_id"].ToString()!="")
				{
					model.dp_id=int.Parse(row["dp_id"].ToString());
				}
				if(row["u_userName"]!=null)
				{
					model.u_userName=row["u_userName"].ToString();
				}
				if(row["u_password"]!=null)
				{
					model.u_password=row["u_password"].ToString();
				}
				if(row["u_trueName"]!=null)
				{
					model.u_trueName=row["u_trueName"].ToString();
				}
				if(row["u_power"]!=null)
				{
					model.u_power=row["u_power"].ToString();
				}
				if(row["u_mobilePhone"]!=null)
				{
					model.u_mobilePhone=row["u_mobilePhone"].ToString();
				}
				if(row["u_email"]!=null)
				{
					model.u_email=row["u_email"].ToString();
				}
				if(row["u_address"]!=null)
				{
					model.u_address=row["u_address"].ToString();
				}
				if(row["u_state"]!=null && row["u_state"].ToString()!="")
				{
					model.u_state=int.Parse(row["u_state"].ToString());
				}
				if(row["u_img"]!=null)
				{
					model.u_img=row["u_img"].ToString();
				}
				if(row["u_leve"]!=null && row["u_leve"].ToString()!="")
				{
					model.u_leve=int.Parse(row["u_leve"].ToString());
				}
				if(row["u_sn"]!=null && row["u_sn"].ToString()!="")
				{
					model.u_sn=int.Parse(row["u_sn"].ToString());
				}
				if(row["u_note"]!=null)
				{
					model.u_note=row["u_note"].ToString();
				}
				if(row["u_createtime"]!=null && row["u_createtime"].ToString()!="")
				{
					model.u_createtime=DateTime.Parse(row["u_createtime"].ToString());
				}
				if(row["u_lastime"]!=null && row["u_lastime"].ToString()!="")
				{
					model.u_lastime=DateTime.Parse(row["u_lastime"].ToString());
				}
				if(row["u_ext1"]!=null)
				{
					model.u_ext1=row["u_ext1"].ToString();
				}
				if(row["u_ext2"]!=null)
				{
					model.u_ext2=row["u_ext2"].ToString();
				}
				if(row["u_ext3"]!=null)
				{
					model.u_ext3=row["u_ext3"].ToString();
				}
				if(row["u_ext4"]!=null)
				{
					model.u_ext4=row["u_ext4"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select uid,pid,rid,dp_id,u_userName,u_password,u_trueName,u_power,u_mobilePhone,u_email,u_address,u_state,u_img,u_leve,u_sn,u_note,u_createtime,u_lastime,u_ext1,u_ext2,u_ext3,u_ext4 ");
			strSql.Append(" FROM Tab_Userinfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" uid,pid,rid,dp_id,u_userName,u_password,u_trueName,u_power,u_mobilePhone,u_email,u_address,u_state,u_img,u_leve,u_sn,u_note,u_createtime,u_lastime,u_ext1,u_ext2,u_ext3,u_ext4 ");
			strSql.Append(" FROM Tab_Userinfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Tab_Userinfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.uid desc");
			}
			strSql.Append(")AS Row, T.*  from Tab_Userinfo T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Tab_Userinfo";
			parameters[1].Value = "uid";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

